<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'

export default {
  name: "Scroll",
  props: {
    //BScroll对象的probeType，是否开启实时监听
    probeType: {
      type: Number,
      default: 0
    },
    //BScroll对象的probeType，是否开启实时监听
    pullUpLoad: {
      type: Boolean,
      default: false
    },
    pullDown: {
      type: Boolean,
      default: false
    },

  },
  data() {
    return {
      scroll: null,
    }
  },
  mounted() {
    //创建BScroll对象
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true,//不设置时，button可以点击，div不能点击
      probeType: this.probeType,//根据父组件是否传值来判断是否需要实时监听
      pullUpLoad: this.pullUpLoad, //是否监听上拉事件
      /*pullDownRefresh: this.pullDown,//下拉刷新页面*/
    });
    //监听滚动的位置
    this.scroll.on('scroll', position => {
      this.$emit('scroll', position)
    })
    //监听上拉事件
    this.scroll.on('pullingUp', () => {
      this.$emit('pullingUp');
    })
    //监听上拉事件
    /*this.scroll.on('pullingDown',()=>{
      console.log("下拉刷新")
      return true;
    })*/

  },
  methods: {
    //页面定位到指定位置
    scrollTo(x, y, time = 600) {
      this.scroll && this.scroll.scrollTo(x, y, time);
    },
    //向上加载更多 完成
    finishPullUp() {
      this.scroll && this.scroll.finishPullUp();
    },
    //重新计算可滚动区域
    refresh() {
      this.scroll && this.scroll.refresh();
    },
    getScrolly() {
      return this.scroll ? this.scroll.y : 0;
    }

  },
}
</script>

<style scoped>

</style>
